package com.uzai.console.service.remoteapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.remoteapi.CheakUsersRelationIdDto;
import com.uzai.console.dto.remoteapi.DirectionalGoodsQuery;
import com.uzai.console.para.CdkeysOldCardExchangeRemoteApiPara;
import com.uzai.console.para.CheckTlbAppkeyAndAppidRemoteApiPara;
import com.uzai.console.service.remoteapi.CdkeysRemoteApiService;
import com.uzai.console.service.remoteapi.TlbRemoteApiService;
import com.uzai.console.vo.remoteapi.cdkey.CdkeyConvertVo;
import com.uzai.console.vo.remoteapi.cps.TlbDirectionalGoodsVo;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推老板平台远程调用接口
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class TlbRemoteApiServiceImpl implements TlbRemoteApiService {

    private static Logger logger = LoggerFactory.getLogger(TlbRemoteApiServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 推老板appId和appkey验证
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param checkTlbAppkeyAndAppidRemoteApiPara
     * @return null
     */
    public boolean checkTlbAppkeyAndAppid(CheckTlbAppkeyAndAppidRemoteApiPara checkTlbAppkeyAndAppidRemoteApiPara) {

        boolean result = false;

        try{
            String tlbServerUrl = uzaiConsoleSysConfig.getTlbServerUrl();
            String remoteUrl = tlbServerUrl + "/api/appid/check";
            Map<String, Object> param = new HashMap<>();
            param.put("appid", checkTlbAppkeyAndAppidRemoteApiPara.getAppid());
            param.put("appkey", checkTlbAppkeyAndAppidRemoteApiPara.getAppkey());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);
            try {
                ResponseEntity<UzaiRespVo> response = restTemplate.postForEntity(remoteUrl, request, UzaiRespVo.class);
                if (response == null || response.getStatusCode() != HttpStatus.OK || response.getBody() == null){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
                }
                UzaiRespVo uzaiRespVo = response.getBody();
                logger.info("--验证推老板appid和appkey remoteApi---remoteUrl={}, para={}, result={}", remoteUrl, JSONObject.toJSONString(param), JSONObject.toJSONString(uzaiRespVo));
                result = Boolean.valueOf(uzaiRespVo.getData().toString());
            }catch (Exception e){
                logger.error("--checkTlbAppkeyAndAppid  request error..{}", e.getMessage());
            }
        }catch (Exception e){
            logger.error("--oldCardExchange convert request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return result;
    }

    /**
     * 分页查询推老板淘宝定向商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param directionalGoodsQuery
     * @return null
     */
    public TlbDirectionalGoodsVo queryTbDirectionalGoods(DirectionalGoodsQuery directionalGoodsQuery) {

        TlbDirectionalGoodsVo tlbDirectionalGoodsVo = null;
        try{
            String tlbServerUrl = uzaiConsoleSysConfig.getTlbServerUrl();
            String remoteUrl = tlbServerUrl + "/directionalGoods/queryDirectionalGoods";
            Map<String, String> param =  BeanUtils.describe(directionalGoodsQuery);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, String>> request = new HttpEntity<>(param, headers);
            ResponseEntity<UzaiRespVo> response = restTemplate.postForEntity(remoteUrl, request, UzaiRespVo.class);
            if (response == null || response.getStatusCode() != HttpStatus.OK || response.getBody() == null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            UzaiRespVo uzaiRespVo = response.getBody();
            logger.info("--远程查询推老板定向商品接口---remoteUrl={}, para={}, result={}", remoteUrl, JSONObject.toJSONString(param), JSONObject.toJSONString(uzaiRespVo));
            logger.info(JSONObject.toJSONString(uzaiRespVo.getData()));
            tlbDirectionalGoodsVo = JSONObject.parseObject(JSONObject.toJSONString(uzaiRespVo.getData()), TlbDirectionalGoodsVo.class);
        }catch (Exception e){
            logger.error("--远程查询推老板定向商品接口 request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return tlbDirectionalGoodsVo;
    }

    /**
     * 验证推老板appid是否绑定了渠道id
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cheakUsersRelationIdDto
     * @return null
     */
    public boolean cheakUsersRelationId(CheakUsersRelationIdDto cheakUsersRelationIdDto) {

        boolean result = false;

        try{
            String tlbServerUrl = uzaiConsoleSysConfig.getTlbServerUrl();
            String remoteUrl = tlbServerUrl + "/directionalGoods/cheakUsersRelationId";
            Map<String, Object> param = new HashMap<>();
            param.put("memberNo", cheakUsersRelationIdDto.getMemberNo());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);
            ResponseEntity<UzaiRespVo> response = restTemplate.postForEntity(remoteUrl, request, UzaiRespVo.class);
            if (response == null || response.getStatusCode() != HttpStatus.OK || response.getBody() == null){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            UzaiRespVo uzaiRespVo = response.getBody();
            logger.info("--验证推老板appid是否绑定了渠道id remoteApi---remoteUrl={}, para={}, result={}", remoteUrl, JSONObject.toJSONString(param), JSONObject.toJSONString(uzaiRespVo));
            result = Boolean.valueOf(uzaiRespVo.getData().toString());
        }catch (Exception e){
            logger.error("--验证推老板appid是否绑定了渠道id request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return result;
    }

}
